/**
 * 
 */
package com.jysz.certificate.persistence.model.system;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.hibernate.annotations.OrderBy;

import com.jysz.webcore.ssh.entity.AbstractBaseEntity;

/**
 * @author <a href="mailto:javaengine@outlook.com">Kaca Rich</a>
 * 
 */
@Entity
@Table(name = "sys_menu_")
@org.hibernate.annotations.Entity
public class Menu extends AbstractBaseEntity<Long> implements IOper{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private String code;
	private String name;
	private String title;
	private String link;
	private String target;
    private Integer grade; //菜单等级  
    private Integer order; //同一级菜单中的顺序
	private Menu parent;
	private List<Menu> childMenu;
	private List<Oper> Opers;
	
	@Column(name = "_name", nullable = false,columnDefinition="varchar(255)")
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Column(name= "_title",columnDefinition="varchar(255)")
	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	@ManyToOne(fetch = FetchType.LAZY)  
	@JoinColumn(name = "_parent_id",columnDefinition="bigint(20)") 
	public Menu getParent() { 
		return parent;
	}

	public void setParent(Menu parent) {
		this.parent = parent;
	}

	@Column(name="_link",columnDefinition="varchar(255)")
	public String getLink() {
		return link;
	}

	public void setLink(String link) {
		this.link = link;
	}

	@Column(name = "_grade",columnDefinition="int(11)")
	public Integer getGrade() {
		return grade;
	}

	public void setGrade(Integer grade) {
		this.grade = grade;
	}

	@Column(name="_order",columnDefinition="int(11)")
	public Integer getOrder() {
		return order;
	}

	public void setOrder(Integer order) {
		this.order = order;
	}

	@Column(name="_target",columnDefinition="varchar(255)")
	public String getTarget() {
		return target;
	}

	public void setTarget(String target) {
		this.target = target;
	}

	@OneToMany(targetEntity = Menu.class, cascade = { CascadeType.ALL }, mappedBy = "parent")  
	@Fetch(FetchMode.SUBSELECT)  
	@OrderBy(clause = "order")
	public List<Menu> getChildMenu() {
		return childMenu;
	}

	public void setChildMenu(List<Menu> childMenu) {
		this.childMenu = childMenu;
	}

	@Column(name="_code", unique = true,columnDefinition="varchar(255)")
	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	
	@OneToMany(targetEntity = Oper.class, cascade = { CascadeType.ALL }, mappedBy = "parent")  
	@Fetch(FetchMode.SUBSELECT)  
	public List<Oper> getOpers() {
		return Opers;
	}

	public void setOpers(List<Oper> opers) {
		Opers = opers;
	}


	/**begin properties column constan**/
	public static final String COLUMN_ORDER = "order";
	public static final String COLUMN_GRADE = "grade";
	public static final String COLUMN_CODE = "code";
	public static final String COLUMN_PARENT = "parent";
	public static final String COLUMN_NAME = "name";
	/**end properties column constan**/

	@Override
	@Transient 
	public List<? extends IOper> getChilds() {
		if(this.getName().equals("驻店管理")){
			System.out.println(this.getOpers().size());
			System.out.println(this.getChildMenu().size());
		}
		return this.getChildMenu() == null||this.getChildMenu().size()==0 ? this.getOpers():this.getChildMenu();
	}

}
